require "Common/define"
require "Logic/Config"
require "Net/send_Msg"

EmailItemList ={}
HelperFunc.SetUIEnv(EmailItemList)
local this = EmailItemList;

this.readCtr = nil;
this.ico = nil;
this.titl = nil;
this.senderNam = nil;
this.sendTim = nil;
this.overTim = nil;
this.itemData = {};
this.bgqualit = nil;
this.framqualit = nil;

function EmailItemList.initData(go,itemValue)
    itemData = {};
    itemData = itemValue;
    readCtrl = go:GetController("readCtrl");
    icon = go:GetChild("icon");       --系统图标
    title = go:GetChild("title");
    senderName = go:GetChild("sender");
    sendTime = go:GetChild("sendTime");
    overTime = go:GetChild("overTime");
    bgquality = go:GetChild("quality");
    framquality = go:GetChild("frame");
    redPointCtrl=go:GetChild("UIRedDots"):GetController("c1")
    --显示每条邮件数据和是否读取状态
    if itemValue ~= nil then
        --redPointCtrl.selectedIndex=2
        if itemValue["icon"] ~= nil then
            icon.url = "ui://ItemAtlas/"..itemValue["icon"];
            if itemValue.quality then
                bgquality.url = EnumConst.AtlasPath.ICONAtlas.. Config.IconQuality[itemValue["quality"]];
                framquality.url =EnumConst.AtlasPath.ICONAtlas.. Config.IconFrame[itemValue["quality"]];
            end
        end
        local dead_time = math.floor(tonumber(itemValue["deadTime"]));
        local haveReward=itemValue["rewards"] ~= nil and #itemValue["rewards"] > 0

        if itemValue["mailRead"] == 0 then
            readCtrl.selectedIndex = 0
        elseif itemValue["mailGetItems"] == 0 and haveReward then
            readCtrl.selectedIndex=1
        else
            readCtrl.selectedIndex=2
        end
        if dead_time > 0 and HelperFunc.GetServerTime() >= dead_time then
            readCtrl.selectedIndex = 2
        end
        title.text = itemValue["mailSubject"];
        if itemValue and itemValue.cfgId == 213 then
            senderName.text = string.format(itemValue["senderName"], itemValue.param["sender"] or "--")
        else
            senderName.text = itemValue["senderName"]
        end
        local sTime = tonumber(itemValue["sendTime"])
        local time = os.date("%Y-%m-%d",math.floor(sTime));
        sendTime.text = time;

        if dead_time == 0 then
            overTime.text = ""
        else
            local str = this.GetTimeStr(dead_time - HelperFunc.GetServerTime())
            overTime.text = str
        end
        go.onClick:Clear();
        go.onClick:Add(function()
            UIEmail.showDetails(itemValue);
        end);
    end
end
function EmailItemList.GetTimeStr(timeSecond)
    if timeSecond <= 0 then
        return "已过期"
    end
    --print(timeSecond)
    local days = Mathf.Floor(timeSecond / (3600 * 24));
    if days > 0 then
        return days .. "天后过期";
    end
    local hours = Mathf.Floor((timeSecond % (3600 * 24)) / 3600);
    if hours > 0 then
        return  hours .. "小时后过期";
    end
    local minutes = Mathf.Floor((timeSecond % (3600 * 24)) % 3600 / 60);
    if minutes > 0 then
        return minutes .. "分钟后过期";
    end
    return "1分钟后过期";
end
return EmailItemList;